<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>document</title>
    <script type="text/javascript" src="../js/vue.js"></script>
</head>
<body>
    <div id="root">
        <h3>人员列表</h3>
        <button @click.once="add">添加一个老刘</button>
        <ul>
            <!--key由vue维护，不会出现在开发者工具中
                不写key时，key默认会用index作为key
                当使用index作为key时，如果进行破坏数据顺序的操作，渲染可能会出问题
                所以一般用数据的唯一标识作为key
            -->
            <li v-for="p in persons" :key="p.id"> 
                {{p.name}}-{{p.age}}
                <input type="text">
            </li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false

        let vm = new Vue({
            el: "#root", 
            data: { 
                persons:[
                    {
                        id: 101,
                        name: '张三',
                        age: 18
                    },
                    {
                        id: 102,
                        name: '李四',
                        age: 20
                    },
                    {
                        id: 103,
                        name: '王五',
                        age: 23
                    }
                ]
            },
            methods: {
                add() {
                    let p = {id: 104, name: "老刘", age: '30'}
                    this.persons.unshift(p)
                }
            },
        })
    </script>
</body>
</html>